import { getItemByClass } from './setting'


export const menuOnElement: StrNumsObject[] = [
  {
    id: 0,
    type: 'warning',
    text: '锁定'
  },
  {
    id: 1,
    type: 'warning',
    text: '复制'
  },
  {
    id: 2,
    type: 'danger',
    text: '解除锁定'
  },
  {
    id: 3,
    type: 'danger',
    text: '删除'
  },
]


export const menuOnEditarea: StrNumsObject[] = [
  {
    id: 0,
    type: 'warning',
    text: '贴上'
  },
]


/**
 * @function clickMenu
 * @param config 配置对象
 * @returns Boolean
 */
export const clickMenu: Function = (ev, targetId: string, parentId: string): void => {
  let parent = document.getElementById(parentId)
  let menu = document.getElementById(targetId)
  if (menu && parent) {
    let bounds = parent.getBoundingClientRect()

    menu['style'].left = ev.pageX - bounds.x + 'px'
    menu['style'].top = ev.pageY - bounds.y + 'px'
  } else console.warn('Drag: _setting_ cant find the menu')
}


/**
 * @function elementOperate
 * @param DRAG 组件池
 * @param _this 组件对象
 * @param behavior 操作类型对象
 * @param drag 拖拽元素集合
 */
export const elementOperate: Function = (DRAG: Map<string, any>, _this: any, behavior: StrNumsObject, drag: AnyObject): null | AnyObject => {
  let element: Element | null = getItemByClass('nymo-selected', /nymo\-selected/g)


  if (element && DRAG.has(element.id)) {
    let dragger: AnyObject = DRAG.get(element.id)
    switch (behavior.id) {
      case 0:

        // 锁定
        dragger.is_able = false
        break;
      case 1:

        // 复制  返回深拷贝
        let copy: AnyObject = JSON.parse(JSON.stringify(dragger))
        copy.class = []
        copy.is_able = true
        copy.isDrag = false
        _this.$message({
          message: `${behavior.text}成功`,
          type: 'success'
        })
        return copy
      case 2:

        // 解除锁定
        if (dragger.is_able) return _this.$message({
          message: `该组件尚未锁定`,
          type: 'error'
        })
        dragger.is_able = true
        break;
      case 3:

        // 删除

        // 获取ID中的对应参数 深拷贝
        let subId_First: string = dragger.id.substring(10, dragger.id.length)
        let subId_Second: string = subId_First.substring(subId_First.indexOf('-') + 1, subId_First.length)
        let elementType: string = subId_Second.substring(0, subId_Second.indexOf('-'))


        DRAG.delete(element.id)
        for (let removeItem = 0; removeItem < drag[elementType].length; removeItem++) {
          if (drag[elementType][removeItem].id === dragger.id) {
            drag[elementType].splice(removeItem, 1)
          }
        }
        element.remove();
        break;
    }

    _this.$message({
      message: `${behavior.text}成功`,
      type: 'success'
    })

  } else {

    _this.$message({
      message: '请先选中组件',
      type: 'error'
    })

  }
  return null
}



/**
 * @function editAreaOperate
 * @param DRAG 组件池
 * @param drag 拖拽元素集合
 * @param _this 组件对象
 * @param behavior 操作类型对象
 * @param copyElement 复制对象
 * @param position 贴上的坐标
 */
export const editAreaOperate: Function = (
  DRAG: Map<string, any>,
  drag: AnyObject,
  _this: any,
  behavior: StrNumsObject,
  copyElement: AnyObject | null,
  position: StrNumsObject | null
): null | AnyObject => {

  if (position && copyElement) {
    switch (behavior.id) {
      case 0:

        // 贴上

        // 获取ID中的对应参数 深拷贝
        let copy: AnyObject = JSON.parse(JSON.stringify(copyElement))
        let subId_First: string = copy.id.substring(10, copy.id.length)
        let subId_Second: string = subId_First.substring(subId_First.indexOf('-') + 1, subId_First.length)
        let elementType: string = subId_Second.substring(0, subId_Second.indexOf('-'))
        let toolsIndex: string = subId_Second.substring(subId_Second.indexOf('-') + 1, subId_Second.length)

        // 修改
        copy.id = `nymo-child${DRAG.get('childNums') + 1}-${elementType}-${toolsIndex}`
        copy.left = position.x
        copy.top = position.y

        drag[elementType].push(copy)
        DRAG.set(copy.id, copy)
        DRAG.set('childNums', DRAG.get('childNums') + 1)
        break;
      case 1:
    }

    _this.$message({
      message: `${behavior.text}成功`,
      type: 'success'
    })

  } else {

    _this.$message({
      message: '请先操作组件',
      type: 'error'
    })

  }
  return null
}